<template>
  <el-form ref="form" :model="form" label-width="80px" :rules="rules">
    <el-form-item label="菜单名称" prop="name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="菜单路径">
      <el-input v-model="form.href"></el-input>
    </el-form-item>
    <el-form-item label="上级菜单">
      <el-select v-model="form.parentId" placeholder="请选择上级菜单">
        <el-option label="无上级菜单" :value="-1"></el-option>
        <el-option v-for="menu in rootMenu"
        :key="menu.id" :label="menu.name" :value="menu.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="form.description"></el-input>
    </el-form-item>
    <el-form-item label="前端图标" prop="icon">
      <el-input v-model="form.icon"></el-input>
    </el-form-item>
    <el-form-item label="是否显示">
      <el-switch v-model="form.shown"></el-switch>
    </el-form-item>
    <el-form-item label="排序" prop="orderNum">
      <el-input v-model.number="form.orderNum"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit" :loading="loading">确定</el-button>
      <el-button @click="onCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// eslint-disable-next-line
import { GetMenuInfo, saveOrUpdateMenu } from '@/services/menu';

export default {
  props: ['id'],
  created() {
    this.loadMenu();
  },
  data() {
    return {
      form: {
        id: 0,
        parentId: -1,
        name: '',
        href: '',
        icon: '',
        orderNum: 0,
        description: '',
        shown: true,
      },
      rules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
        ],
        description: [
          { required: true, message: '请输入菜单描述', trigger: 'blur' },
        ],
        icon: [
          { required: true, message: '请输入图标名称', trigger: 'blur' },
        ],
        orderNum: [
          { required: true, message: '排序不能为空' },
          { type: 'number', message: '排序必须为数字值', trigger: 'change' },
        ],
      },
      rootMenu: [],
      loading: false,
    };
  },
  methods: {
    async loadMenu() {
      const { data } = await GetMenuInfo(this.id);
      if (data.code === '000000') {
        if (this.id) {
          console.log(data.data.menuInfo.id);
          Object.keys(this.form).forEach((key) => {
            this.form[key] = data.data.menuInfo[key];
          });
        }
        this.rootMenu = data.data.parentMenuList;
      }
    },
    onCancel() {
      this.$router.push({ name: 'menu' });
    },
    async onSubmit() {
      this.$confirm('是否提交数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        try {
          await this.$refs.form.validate((valid) => {
            if (!valid) {
              throw new Error('error submit');
            }
            return true;
          });
          this.loading = true;
          if (this.form.id === 0) {
            const menu = {};
            Object.assign(menu, this.form);
            delete menu.id;
            const { data } = await saveOrUpdateMenu(menu);
            if (data.code === '000000') {
              this.$message.success('菜单创建成功');
              this.$router.push({ name: 'menu' });
            } else {
              this.$message.error('菜单创建失败');
            }
          } else {
            const { data } = await saveOrUpdateMenu(this.form);
            if (data.code === '000000') {
              this.$message.success('菜单修改功');
              this.$router.push({ name: 'menu' });
            } else {
              this.$message.error('菜单修改失败');
            }
          }
          this.loading = false;
        } catch (error) {
          console.log('校验未通过');
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消提交',
        });
      });
    },
  },
};
</script>

<style scoped>
.el-form {
  padding: 20px;
  background-color: #fff;
}
</style>
